// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.render;

import java.io.IOException;
import java.io.PrintWriter;

import lichen.hibernate.SessionManaged;

import org.apache.commons.logging.Log;
import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.internal.services.PageMarkupRenderer;
import org.apache.tapestry.internal.services.PageResponseRenderer;
import org.apache.tapestry.internal.structure.Page;
import org.apache.tapestry.ioc.OrderedConfiguration;
import org.apache.tapestry.ioc.annotations.InjectService;
import org.apache.tapestry.services.MarkupWriterFactory;
import org.apache.tapestry.services.Request;
import org.apache.tapestry.services.RequestFilter;
import org.apache.tapestry.services.RequestGlobals;
import org.apache.tapestry.services.RequestHandler;
import org.apache.tapestry.services.Response;

/**
 * 针对render的处理的模块.
 * @author Jun Tsai
 * @version $Revision$
 * @since 0.0.3
 */
public class RenderModule {
	/** 页面采用的Content type **/
	protected static final String CONTENT_TYPE = "text/html; charset=UTF-8";
	/**
	 * 页面解析采用utf-8方式.
	 * @param markupRenderer markup renderer
	 * @param markupWriterFactory writer factory
	 * @param delegate delegate 代理类
	 * @return page response renderer
	 */
	public static PageResponseRenderer decoratePageResponseRenderer(
			@InjectService("PageMarkupRenderer")
			final PageMarkupRenderer markupRenderer,
			@InjectService("MarkupWriterFactory")
			final MarkupWriterFactory markupWriterFactory, final Object delegate) {
		return new PageResponseRenderer() {
			public void renderPageResponse(Page page, Response response)
					throws IOException {
				MarkupWriter writer = markupWriterFactory.newMarkupWriter();
				markupRenderer.renderPageMarkup(page, writer);
				PrintWriter pw = response
						.getPrintWriter(CONTENT_TYPE);
				writer.toMarkup(pw);
				pw.flush();
			}
		};
	}
	 /**
	  * 表单输入也采用UTF-8信息
	  * @param configuration 配置.
	  * @param encodingFilter 编码的过滤器.
	  */
   public void contributeRequestHandler(  
           OrderedConfiguration<RequestFilter> configuration,  
           @InjectService("EncodingFilter")  
           RequestFilter encodingFilter) {  
       configuration.add("EncodingFilter", encodingFilter);  
   }  
   /**
    * 创建一个编码的过滤器
    * @param requestGlobals 全局请求的Request.
    * @return 请求过滤器.
    */
   public RequestFilter buildEncodingFilter(final Log log,final RequestGlobals requestGlobals,final SessionManaged sessionManaged) {
   	
       return new RequestFilter() {  
           public boolean service(Request request, Response response,  
                   RequestHandler handler) throws IOException {  
           	//触发线程级的service的load,保证整个请求为一个session.
           	sessionManaged.getSession();
           	
               requestGlobals.getHTTPServletRequest().setCharacterEncoding(  
                       "UTF-8");  
               	return handler.service(request, response);
           }
       };
   }   
   

}
